package com.qianxp.designparttern.prototype;

import java.util.ArrayList;
import java.util.List;


/**<p>
 *  1.反射。通过beanUtils 浅拷贝 
 *  2.实现clone接口 浅拷贝 
 *  3.序列化、反序列化 深拷贝
 * <p>
 */
/**
 *@author qianxp
 */
public class PrototypeTest {
    public static void main(String[] args) {
        // testBeanUtils();
         testClone();
         testDeepClone();

    }
    /**
    private static void testBeanUtils() {
        Student stu1 = initStudent();
        Student stu2 = new Student();
        BeanUtils.copyProperties(stu1, stu2);
        testIfSame(stu1, stu2);

    }
     */

    private static void testClone() {
        Student stu1 = initStudent();
        Student stu2;
        try {
            stu2 = (Student) stu1.clone();
            testIfSame(stu1,stu2);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
      
    }

    private static void testDeepClone() {
        Student stu1 = initStudent();
        Student stu2;
        try {
            stu2 = (Student) stu1.deepClone();
            testIfSame(stu1,stu2);
        } catch (Exception e) {
            e.printStackTrace();
        }
      
    }


    private static Student initStudent(){
        Student stu = Student.getInstance();
        stu.setId(1L);
        stu.setSex("男");
        stu.setCardNo("card.no.001");
        stu.setName("张三");
        stu.setAddress("武汉");
        List<Book> books = new ArrayList<>();
        Book book = new Book();
        book.setId(001L);
        book.setBookNo("book_001");
        book.setBookSheet("bookSheet");
        book.setName("红与黑");
        books.add(book);
        stu.setBooks(books);
        return stu;
    }

    private static void testIfSame( Student stu1, Student stu2){
        System.out.println("object " + (stu1 == stu2));
        System.out.println("books " + (stu1.getBooks() == stu2.getBooks()));
        System.out.println(stu1.toString());
        System.out.println(stu2.toString());
    }
}